package com.ysx.attendance.service.impl;

import com.ysx.attendance.bean.ClassBean;
import com.ysx.attendance.bean.StudentBean;
import com.ysx.attendance.bean.TeacherBean;
import com.ysx.attendance.dao.ClassDao;
import com.ysx.attendance.entity.ClassEntity;
import com.ysx.attendance.service.ClassService;
import com.ysx.attendance.utils.DaoFactory;

import java.util.ArrayList;
import java.util.List;

public class ClassServiceImpl  implements ClassService {


    private ClassDao classDao=null;

    @Override
    public ClassBean findTeacherAndStudentListByClassIdAndStudentName(Integer classId, String studentName) {
        ClassBean classBean = findTeacherAndStudentListByClassId(classId);

        List<StudentBean> rs = new ArrayList<>();
        List<StudentBean> studentBeanList = classBean.getStudentBeanList();
        for(StudentBean studentBean:studentBeanList){
            if(studentName.equals(studentBean.getStudentName())){
                rs.add(studentBean);
            }
        }
        classBean.setStudentBeanList(rs);
        return classBean;
    }

    @Override
    public ClassBean findTeacherAndStudentListByClassId(Integer classId) {
        DaoFactory<ClassDao> daoFactory = new DaoFactory();
        classDao =daoFactory.getDao(ClassDao.class);

        // 根据班级ID组装最终数据给Controller
        ClassBean  rs = new ClassBean();
        if(null == classId){
            return rs;
        }


        // 1 查班级
        ClassBean classBean= classDao.findClassByClassId(classId);
        String className = classBean.getClassName();
        rs.setClassId(classId);
        rs.setClassName(className);

        // 2 查老师
        TeacherBean teacherBean = classDao.findTeacherBeanByClassId(classId);
        rs.setTeacherBean(teacherBean);

        // 3 查学生
        List<StudentBean> studentBeanList = classDao.findStudentBeanListByClassId(classId);
        rs.setStudentBeanList(studentBeanList);


        return rs;
    }


    /**
     * 查所有的班级
     * @return
     */
    @Override
    public List<ClassBean> findClassList() {
        DaoFactory<ClassDao> daoFactory = new DaoFactory();
        classDao =daoFactory.getDao(ClassDao.class);
        List<ClassBean> classList = classDao.findClassList();

        List<ClassBean> rs = new ArrayList<>();

        for(ClassBean classBean: classList){

            ClassBean clzBean= findTeacherAndStudentListByClassId(classBean.getClassId());
            rs.add(clzBean);
        }
        return rs;
    }

    @Override
    public List<ClassEntity> findAllClass() {
        DaoFactory<ClassDao> daoFactory = new DaoFactory();
        classDao =daoFactory.getDao(ClassDao.class);
        // ClassDao classDao= DaoUtil.getClassDao();
        return classDao.findAllClass();
    }

    @Override
    public ClassEntity findClassById(Integer id) {
        DaoFactory<ClassDao> daoFactory = new DaoFactory();
        classDao =daoFactory.getDao(ClassDao.class);
        return classDao.findById(id);
    }

}
